#include "mysqlDataBase.h"


mysqlDataBase::mysqlDataBase(const string & host, const string & user, const string & passwd)
{
    m_conn = mysql_init(nullptr);

    m_host = host.c_str();
    m_user = user.c_str();
    m_passwd = passwd.c_str();
}

mysqlDataBase::~mysqlDataBase()
{
    if(m_conn != nullptr)
    {
        close();
    }
    
}

/* 连接数据库 */
bool mysqlDataBase::connectDB(const string & connStr)
{

    unsigned int port = 3306;
    if(mysql_real_connect(m_conn, m_host.c_str(), m_user.c_str(), m_passwd.c_str(), connStr.c_str() , port, NULL, 0) == NULL)
    {
        std::cout << "mysql connect error." << std::endl;
        return false;
    }
    // std::cout << "mysql connect success." << std::endl;
    return true;
}

/* 执行sql语句(exec) */
bool mysqlDataBase::executeSql(const string &sql)
{
    if(mysql_query(m_conn, sql.c_str()))
    {
        std::cout << "file:" << __FILE__ << ",lines:" << __LINE__ << mysql_error(m_conn) << std::endl;
        return false;
    }
    return true;
}

/* 制定Sql查询语句（select）*/
QueryResult mysqlDataBase::querySql(const string &sql)
{
    QueryResult result;
    executeSql(sql);

    /* 从结果集里面拿取数据 */
    MYSQL_RES * res = mysql_store_result(m_conn);
    if(res == NULL)
    {
        std::cout << "file:" << __FILE__ << ",lines:" << __LINE__ << mysql_error(m_conn) << std::endl;
        return result;
    }

    /* 获取列数 */
    unsigned int columns = mysql_num_fields(res);
    unsigned int rows = mysql_num_rows(res);

    std::cout << "columns:" << columns << std::endl;
    std::cout << "rows:" << rows << std::endl;

    /* 获取字段的名称:列名 */
    MYSQL_FIELD * fields = mysql_fetch_fields(res);

    /* 打印所有列名 */
    for(int idx = 0; idx < columns; idx++)
    {
        result.columnName.push_back(fields[idx].name);
        std::cout << " " << result.columnName[idx] << "\t";
    }
    std::cout << std::endl;

    /* 获取每一行的数据 */
    MYSQL_ROW row;
    while((row = mysql_fetch_row(res)) != NULL)
    {
        vector<string> tmpRow;
        for(int idx = 0; idx < columns; idx++)
        {
            const char * tmp = NULL;
            if(row[idx] != NULL)
            {
                tmp = row[idx];
                std::cout << tmp << "\t";
            }
            else
            {
                tmp = "NULL";
            }
            tmpRow.push_back(tmp);
        }
        std::cout << std::endl;
        result.rows.push_back(tmpRow);
        
    }
    /* 释放结果集 */
    mysql_free_result(res);

    return result;
}

/* 关闭数据库连接 */
void mysqlDataBase::close()
{
    mysql_close(m_conn);
    m_conn = nullptr;
}


/* 刷新时间戳 */
void mysqlDataBase::refreshAliveTime()
{
    /* todo... */
    /* 得到当前的时间 */
    m_backCurrentTime = chrono::steady_clock::now();
}

/* 获取数据库句柄的存活时间 */
long mysqlDataBase::getAliveTime()
{
    /* 纳秒 */
    chrono::nanoseconds res = chrono::steady_clock::now() - m_backCurrentTime;
    /* 精度转换 */
    chrono::seconds sec = chrono::duration_cast<chrono::seconds>(res);
    /* 返回秒 */
    return sec.count();
}